# import base64
# import subprocess
# import time
# from io import BytesIO
#
# from PIL import Image
# from selenium import webdriver
# from selenium.webdriver import ActionChains, DesiredCapabilities
# import undetected_chromedriver as uc
# from selenium.webdriver.chrome.options import Options
# from selenium.webdriver.support.wait import WebDriverWait
#
# from eshoputils.browser_utils import getEleWait, getElesNoWait, getEleNoWait
# from eshoputils.utils import get_outlook_verify_code, detect_displacement
#
#
# class ShopeeSpider:
#     def __init__(self, urls):
#         self.urls = urls
#
#         chrome_options = uc.ChromeOptions()
#
#         #chrome_options = Options()
#         # chrome_options.add_experimental_option("detach", True)
#         # chrome_options.add_experimental_option('excludeSwitches',['enable-automation'])
#         # chrome_options.binary_location=r"C:\Program Files\Google\Chrome\Application\chrome.exe"
#         #chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36')
#         #chrome_options.add_argument('user-agent=Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.93 Mobile Safari/537.36')
#         #无痕模式
#         #chrome_options.add_argument('--incognito')
#         prefs = {
#             'profile.default_content_setting_values':{
#                 'notifications':2
#             }
#         }
#         chrome_options.add_experimental_option('prefs', prefs)
#         #chrome_options.add_argument(r'--user-data-dir=C:\Users\18222\AppData\Local\Google\Chrome\seleniumdata')
#         # # chrome_options.add_argument('--headless')
#         # # chrome_options.add_argument('--disable-gpu')
#         chrome_options.add_argument("--no-first-run")
#         chrome_options.add_argument("--no-default-browser-check")
#         chrome_options.add_argument("--allow-running-insecure-content")
#         chrome_options.add_argument('--disable-blink-features=AutomationControlled')
#         #chrome_options.add_argument("--load-extension=D:/code/epchoai/sun_-rpa/chrome-bridge-ex")
#         # chrome_options.add_argument('--lang=tl')
#         # chrome_options.add_argument('--accept-lang=en-US')
#         # capabilities = DesiredCapabilities.CHROME.copy()
#         # capabilities["goog:chromeOptions"] = {"detach": True}
#
#         # chrome_service = webdriver.ChromeService(executable_path="driver/undetected_chromedriver.exe", log_output=subprocess.STDOUT, port=9515)
#         # self.browser = webdriver.Chrome(options=chrome_options, service=chrome_service)
#         # with open("driver/steal1.js") as f:
#         #     self.js = f.read()
#         # self.browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
#         #     "source": self.js
#         # })
#
#
#         #self.browser = uc.Chrome(desired_capabilities=capabilities,user_data_dir=r"C:\Users\18222\AppData\Local\Google\Chrome\seleniumdata",debug=True,headless=True, use_subprocess=False, driver_executable_path="/driver/chromedriver.exe", browser_executable_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe")
#         self.browser = uc.Chrome(options=chrome_options, user_data_dir=r"C:\Users\18222\AppData\Local\Google\Chrome\seleniumdata",debug=True,headless=False, use_subprocess=True, driver_executable_path="driver/undetected_chromedriver.exe", browser_executable_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe")
#         #self.browser = uc.Chrome(options=chrome_options,debug=True,headless=False, use_subprocess=False, driver_executable_path="driver/chromedriver.exe", browser_executable_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe")
#         # self.browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
#         #     "source": "navigator.userAgent=Mozilla/5.0 (Linux; Android 8.1.0; CPH1803 Build/OPM1.171019.026; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36 Shopee Beeshop locale/id version=307 appver=24820 rnver=1576757073"
#         # })
#         # self.browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
#         #     "source": "console.log = function(){}"
#         # })
#         self.browser.maximize_window()
#         self.waiter = WebDriverWait(self.browser, 3)
#
#     def enter_shop_v2(self):
#         """
#         登陆先不做，因为登陆需要短信校验，目前先吧登陆后的校验之类的做了
#         :return:
#         """
#         all_product_path = []
#         for url in self.urls:
#             self.browser.get(url[0])
#             # self.browser.execute_script(f'window.open("{url[0]}")')
#             # self.browser.switch_to.window(self.browser.window_handles[1])
#             time.sleep(5)
#
#
#
#
#     def enter_shop(self):
#         all_product_paths = []
#         for url in self.urls:
#             self.browser.execute_script(f'window.open("{url[0]}")')
#             time.sleep(5)
#             login_button = getEleWait(self,'//button[@class="vvOL40 iesrPs AsFRg8 qCI4rz ZKayWA AnY7KS"]')
#             if login_button:
#                 print("需要登陆")
#                 username_input = getEleWait(self,'//*[@id="main"]/div/div[2]/div/div/div/div[2]/div/div[2]/form/div[1]/div[1]/input')
#                 password_input = getEleWait(self,'//*[@id="main"]/div/div[2]/div/div/div/div[2]/div/div[2]/form/div[2]/div[1]/input')
#                 time.sleep(3)
#                 username_input.send_keys("jsxlavpgts@outlook.com")
#                 password_input.send_keys("Aa123456")
#                 print("登陆按钮点击")
#                 login_button.click()
#                 time.sleep(2)
#                 self.browser.refresh()
#                 email_verify_button = getEleWait(self,'//button[@aria-label="Verify by Email OTP"]')
#                 if email_verify_button:
#                     print("需要邮箱校验")
#                     email_verify_button.click()
#                     email_continue_button = getEleWait(self,'//div[@class="DD5hWO jHpdhI nWIhrW"]')
#                     while email_continue_button:
#                         print("邮箱校验前的机器人滑动校验")
#                         drag_pic = getEleWait(self,'//img[@class="wFUSCy" and @draggable="true"]')
#                         background_pic = getEleWait(self,'//img[@class="QxZbFO" and @draggable="false"]')
#                         drag_pic_base64 = drag_pic.get_attribute("src")
#                         background_pic_base64 = background_pic.get_attribute("src")
#                         drag_pic_base64 = drag_pic_base64.split(";")[1].split(",")[1]
#                         background_pic_base64 = background_pic_base64.split(";")[1].split(",")[1]
#                         drag_img_data = base64.b64decode(drag_pic_base64)
#                         background_pic_data = base64.b64decode(background_pic_base64)
#                         drag_image = Image.open(BytesIO(drag_img_data))
#                         background_image = Image.open(BytesIO(background_pic_data))
#                         drag_image.save(r"C:\Users\18222\Desktop\drag.png")
#                         background_image.save(r"C:\Users\18222\Desktop\background.png")
#                         print(f"拖转图片的xy坐标：{drag_pic.location['x']},{drag_pic.location['y']}")
#                         print(f"背景图片的xy坐标：{background_pic.location['x']},{background_pic.location['y']}")
#                         #计算图片距离后拖动
#                         offset = detect_displacement(r"C:\Users\18222\Desktop\background.png", r"C:\Users\18222\Desktop\drag.png")
#                         print(f"滑动距离为:{offset}")
#                         self.slide_by_offset(email_continue_button, offset)
#                         time.sleep(2)
#                         email_continue_button = getEleWait(self, '//div[@class="DD5hWO jHpdhI nWIhrW"]')
#                     #获取邮件
#                     verify_code_input = getEleWait(self, '//*[@id="main"]/div/div[2]/div/div/div/form/div/div[4]/div/input')
#                     if verify_code_input:
#                         time.sleep(5)
#                         print("拖动校验成功进入邮箱验证码输入部分")
#                         print("登陆邮箱获取校验码")
#                         time.sleep(1)
#                         verify_encode = get_outlook_verify_code()
#                         counter = 0
#                         while not verify_encode:
#                             counter += 1
#                             time.sleep(3)
#                             verify_encode = get_outlook_verify_code()
#                             if counter >=3:
#                                 break
#                         if verify_encode:
#                             print(f'邮箱校验码为{verify_encode}')
#                             verify_code_input.send_keys(verify_encode)
#                         else:
#                             raise Exception("没有收到虾皮校验码无法登陆")
#                         verify_btn = getEleWait(self, "//button[@class='vvOL40 iesrPs AsFRg8 qCI4rz TjDXkH ZKayWA AnY7KS']")
#                         verify_btn.click()
#
#             # origin_window = self.browser.current_window_handle
#             # ActionChains(self.browser).click(login_button).perform()
#             # self.waiter.until(expected_conditions.number_of_windows_to_be(2))
#             # new_window = None
#             # for wh in self.browser.window_handles:
#             #     if wh != origin_window:
#             #         new_window = wh
#             #         break
#             # self.browser.switch_to.window(new_window)
#             # new_waiter = WebDriverWait(self.browser, 10)
#             # input = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH, '//*[@id="identifierId"]')
#             # ))
#             # input.send_keys("zhupaizhiduozhi")
#             # next = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH,'//*[@id="identifierNext"]/div/button/span')
#             # ))
#             # next.click()
#             # passinput = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')
#             # ))
#             # time.sleep(2)
#             # passinput.send_keys("Csvcn@112711")
#             # pnext = self.browser.find_element(By.XPATH,'//*[@id="passwordNext"]/div/button/span')
#             # pnext.click()
#             # continuebutton = new_waiter.until(expected_conditions.presence_of_element_located(
#             #     (By.XPATH,'//*[@id="yDmH0d"]/c-wiz/div/div[3]/div/div/div[2]/div/div/button/div[3]')
#             # ))
#             # ActionChains(self.browser).click(continuebutton).perform()
#             # self.browser.switch_to.window(origin_window)
#
#     def slide_by_offset(self, btn, offset):
#         ActionChains(self.browser).click_and_hold(btn).perform()
#         for i in [5] * (offset // 5):
#             ActionChains(self.browser).move_by_offset(i,0).perform()
#         time.sleep(1)
#         ActionChains(self.browser).release(btn).perform()
#
#
